<script setup>
import { defineProps } from 'vue'

const props = defineProps({
  data: {
    type: Array,
    required: true
  }
})

const emit = defineEmits(['run', 'rerun'])

const handleRun = (taskId) => {
  emit('run', taskId)
}

const handleRerun = (taskId) => {
  emit('rerun', taskId)
}
</script>

<template>
  <div class="task-table">
    <table>
      <thead>
        <tr>
          <th>父任务编号</th>
          <th>运行系统</th>
          <th>提交时间</th>
          <th>排队时间</th>
          <th>开始时间</th>
          <th>结束时间</th>
          <th>CPU占用</th>
          <th>内存占用(GB)</th>
          <th>进度</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="task in data" :key="task.parentId">
          <td>{{ task.parentId }}</td>
          <td>{{ task.os }}</td>
          <td>{{ task.submitTime }}</td>
          <td>{{ task.queueTime }}</td>
          <td>{{ task.startTime }}</td>
          <td>{{ task.endTime }}</td>
          <td>{{ task.cpu }}</td>
          <td>{{ task.memory }}</td>
          <td>
            <div class="progress-bar">
              <div 
                class="progress-fill"
                :style="{ width: `${task.progress}%` }"
              ></div>
              <span class="progress-text">{{ task.progress }}%</span>
            </div>
          </td>
          <td>
            <button 
              class="run-btn"
              @click="handleRun(task.parentId)"
            >运行</button>
            <button
              class="rerun-btn"
              @click="handleRerun(task.parentId)"
            >再次运行</button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<style scoped>
.task-table {
  margin-top: 20px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  overflow: hidden;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #ebeef5;
}

th {
  background-color: #f5f7fa;
  font-weight: 600;
  color: #606266;
}

button {
  padding: 5px 12px;
  border-radius: 3px;
  cursor: pointer;
  margin-right: 8px;
  border: 1px solid transparent;
  transition: all 0.3s;
}

.run-btn {
  background-color: #409eff;
  color: white;
}

.run-btn:hover {
  background-color: #66b1ff;
}

.rerun-btn {
  background-color: #67c23a;
  color: white;
}

.rerun-btn:hover {
  background-color: #85ce61;
}

.progress-bar {
  position: relative;
  height: 24px;
  background-color: #f5f7fa;
  border-radius: 12px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background-color: #409eff;
  transition: width 0.3s ease;
  border-radius: 12px 0 0 12px;
}

.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 12px;
  color: #fff;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
}
</style>
